﻿/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/24/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoContentProduct
    {
        public daoContentProduct()
        {

        }        
        private ContentProduct Load(IDataReader de)
        {
            ContentProduct item = new ContentProduct();
            item.Content = clsConvert.DbToString("Content", de);
            item.MetaDescription = clsConvert.DbToString("MetaDescription", de);
            item.PageId = clsConvert.DbToInt("PageId", de);
            item.Language = clsConvert.DbToInt("Language", de);
            item.Title = clsConvert.DbToString("Title", de);
            item.PageURL = clsConvert.DbToString("PageURL", de);
            item.MetaKeywords = clsConvert.DbToString("MetaKeywords", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            item.Language = clsConvert.DbToInt("Language", de);
            return item;            
        }
        public int Insert(ContentProduct objContentProduct)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[9];
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEURL, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objContentProduct.PageURL);
                para[1] = new SqlParameter(busContentProduct.NAME, SqlDbType.NVarChar);
                para[1].Value = objContentProduct.Name;
                para[2] = new SqlParameter(busContentProduct.TITLE, SqlDbType.NVarChar);
                para[2].Value = objContentProduct.Title;
                para[3] = new SqlParameter(busContentProduct.CONTENT, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objContentProduct.Content);
                para[4] = new SqlParameter(busContentProduct.METADESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objContentProduct.MetaDescription);
                para[5] = new SqlParameter(busContentProduct.METAKEYWORDS, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objContentProduct.MetaKeywords);
                para[6] = new SqlParameter(busContentProduct.CREATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objContentProduct.CreatedBy);
                para[7] = new SqlParameter(busContentProduct.CREATEDDATE, SqlDbType.DateTime);
                para[7].Value = objContentProduct.CreatedDate;
                para[8] = new SqlParameter(busContentPage.LANGUAGE, SqlDbType.TinyInt);
                para[8].Value = objContentProduct.Language;
                result = clsSQL.SPExecute("ContentProduct_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentProduct", "Insert(ContentProduct objContentProduct)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }            
            return result;
        }

        public bool OrderUp(int PageId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEID, SqlDbType.Int);
                para[0].Value = PageId;
                result = clsSQL.SPExecute("ContentProduct_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentProduct", "ContentProduct_OrderUp(int PageId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        /// <summary>
        /// Move item down
        /// </summary>
        /// <param name="LogoID"></param>
        /// <returns></returns>
        public bool OrderDown(int PageId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEID, SqlDbType.Int);
                para[0].Value = PageId;
                result = clsSQL.SPExecute("ContentProduct_OrderDown", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentProduct", "OrderDown(int PageId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int pageID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEID, SqlDbType.Int);
                para[0].Value = pageID;
                result = clsSQL.SPExecute("ContentProduct_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentProduct", "Delete(int pageID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public ContentProduct GetByID(int pageID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            ContentProduct item = new ContentProduct();
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEID, SqlDbType.Int);
                para[0].Value = pageID;
                using (de = clsSQL.SPDataReader("ContentProduct_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoContentProduct", " GetByID(int pageID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public ContentProduct GetByURL(string url, int Language)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            ContentProduct item = new ContentProduct();
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEURL, SqlDbType.VarChar);
                para[0].Value = url;
                para[1] = new SqlParameter(busContentProduct.LANGUAGE, SqlDbType.Int);
                para[1].Value = Language;
                using (de = clsSQL.SPDataReader("ContentProduct_GetByURL", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoContentProduct", "GetByURL(string url)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public int Update(ContentProduct objContentProduct)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];
            try
            {
                para[0] = new SqlParameter(busContentProduct.PAGEURL, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objContentProduct.PageURL);
                para[1] = new SqlParameter(busContentProduct.NAME, SqlDbType.NVarChar);
                para[1].Value = objContentProduct.Name;
                para[2] = new SqlParameter(busContentProduct.TITLE, SqlDbType.NVarChar);
                para[2].Value = objContentProduct.Title;
                para[3] = new SqlParameter(busContentProduct.CONTENT, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objContentProduct.Content);
                para[4] = new SqlParameter(busContentProduct.METADESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objContentProduct.MetaDescription);
                para[5] = new SqlParameter(busContentProduct.METAKEYWORDS, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objContentProduct.MetaKeywords);
                para[6] = new SqlParameter(busContentProduct.UPDATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objContentProduct.UpdatedBy);
                para[7] = new SqlParameter(busContentProduct.UPDATEDDATE, SqlDbType.DateTime);
                para[7].Value = objContentProduct.UpdatedDate;              
                para[8] = new SqlParameter(busContentProduct.PAGEID, SqlDbType.Int);
                para[8].Value = objContentProduct.PageId;
                para[9] = new SqlParameter(busContentPage.LANGUAGE, SqlDbType.TinyInt);
                para[9].Value = objContentProduct.Language;
                result = clsSQL.SPExecute("ContentProduct_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentProduct", "Update(ContentProduct objContentProduct)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public ContentProductCollection List(ContentProduct objContentProduct)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            ContentProductCollection items = new ContentProductCollection();
            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objContentProduct.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objContentProduct.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objContentProduct.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = objContentProduct.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objContentProduct.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("ContentProduct_List", para);
                while (de.Read())
                {
                    ContentProduct item = Load(de);
                    items.Add(item);
                }
                de.Close();
                objContentProduct.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoContentProduct", "List(TipNews objTipNews)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }

        public ContentProductCollection GetAll(ContentProduct objContentProduct)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            ContentProductCollection items = new ContentProductCollection();
            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objContentProduct.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objContentProduct.OrderDirection;
                de = clsSQL.SPDataReader("ContentProduct_GetAll", para);
                while (de.Read())
                {
                    ContentProduct item = Load(de);
                    items.Add(item);
                }
                de.Close();              
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoContentProduct", "GetAll(ContentProduct objContentProduct)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }      

    }
}


